package com.uphone.kingmall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.amap.api.maps.model.LatLng;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.HttpParams;
import com.uphone.kingmall.activity.ShopDetailActivity;
import com.uphone.kingmall.adapter.ShopsListAdpater;
import com.uphone.kingmall.base.BaseListFragment;
import com.uphone.kingmall.bean.CircleShopsListBean;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.SharedPreferencesHelper;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/19
 *     desc   :
 * </pre>
 */
public class ShopsListFragment extends BaseListFragment<CircleShopsListBean, CircleShopsListBean.DataBean> {

    private int index = 1;
    private int id = -1;
    private int type = -1;
    private String url = "";
    public final static int TYPE_CRICLE_CAT = 0X0011;
    public final static int TYPE_CRICLE_SHOP = 0X0022;
    public final static int TYPE_CRICLE_SEARCH = 0X0033;
    private String city = "";
    private LatLng latLng;
    private String asc = "2";
    private String searchKey = "";

    public static ShopsListFragment newInstance(int index, int id, int type, String url) {
        Bundle args = new Bundle();
        args.putInt("index", index);
        args.putInt("id", id);
        args.putInt("type", type);
        args.putString("url", url);
        ShopsListFragment fragment = new ShopsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static ShopsListFragment newInstance(int index, String searchKey, int type, String url) {
        Bundle args = new Bundle();
        args.putInt("index", index);
        args.putString("searchKey", searchKey);
        args.putInt("type", type);
        args.putString("url", url);
        ShopsListFragment fragment = new ShopsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView(View view) {
        index = getArguments().getInt("index");
        id = getArguments().getInt("id");
        type = getArguments().getInt("type");
        url = getArguments().getString("url");
        searchKey = getArguments().getString("searchKey");

    }

    @Override
    protected CircleShopsListBean getBeans() {
        return new CircleShopsListBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        if (type == TYPE_CRICLE_SHOP) {
            params.put("circleType", id + "");
        } else if (type == TYPE_CRICLE_CAT) {
            params.put("catId", id + "");
        } else if (type == TYPE_CRICLE_SEARCH) {
            params.put("shopName", searchKey);
        }
        params.put("page", page + "");
        params.put("limit", "10");
        params.put("orderby", index + "");
        if (index == 4) {
            if (TextUtils.equals("2", asc)) {
                params.put("asc", "1");
            } else {
                params.put("asc", "2");
            }
        } else {
            params.put("asc", asc);
        }
        if (latLng != null) {
            params.put("lonlat", latLng.longitude + "," + latLng.latitude);
        }
        if (!TextUtils.isEmpty(SharedPreferencesHelper.getCity())) {
            city = SharedPreferencesHelper.getCity();
        }
        params.put("city", city);
        return params;
    }


    @Override
    protected String getUrl() {
        return url;
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {
        CommonUtil.startIntent(getActivity(), ShopDetailActivity.class, dataList.get(position).getShopId());
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new ShopsListAdpater(1);
    }

    public void reload(String city, LatLng latLng, int page) {
        this.city = city;
        this.latLng = latLng;
        loadData(page);
    }

    public void reload(boolean selected, String searchKey, int page) {
        if (selected) {
            asc = "1";
        } else {
            asc = "2";
        }
        this.searchKey = searchKey;
        loadData(page);
    }


    public void reload(boolean selected, int page) {
        LogUtils.e("selected:" + selected);
        if (selected) {
            asc = "1";
        } else {
            asc = "2";
        }
        loadData(page);
    }

    public void reload(String searchKey, int page) {
        this.searchKey = searchKey;
        loadData(page);
    }

}
